<?php
namespace app\Login\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Index extends Controller
{
        // 加载登录页面
    public function index()
    {
        return $this->fetch('login/login');
    }


    // 登陆仓储cession
    public function login(Request $request)
    {
        $info = $request->post();
        $info['userpass']=MD5($info['userpass']);
        
        $list = db('user')->where(['username' => $info['username'],'userpass'=>$info['userpass']])->find();

        if (!empty($list)) {
            
            Session::set('name',$list['username']);
            Session::set('id',$list['id']);
            $id =  Session::get('id');
            //$list 查询的数据库资料
            $list = Db::view('node','mname,aname')
            ->view('role_node','nid','node.id=role_node.nid and node.status=1')
            ->view('user_role','rid','role_node.rid=user_role.rid')
            ->view('user','id','user.id=user_role.uid')
            ->where('id','=',$id)->select();
            

            // var_dump($list);die;
            

            foreach ($list as $key => $val) {
                $list[$key]['mname'] = ucfirst($val['mname']);
            }

            // var_dump($list);die;

            $nodelist = array();
            foreach($list as $v){
                $nodelist[$v['mname']][] = $v['aname'];
            }

            //数据库
            // var_dump($nodelist);die;
            Session::set('qx',$nodelist);


            return $this->success('登陆成功',url('admin/user/index'));
        }else{
            return $this->error('请先登录',url('admin/index/index'));
        }



        
    }

    // 退出登陆清除session
    public function outlogin()
    {    
        
        $sql=Db::name('');
        Session::delete('qx');
        Session::delete('name');
        return $this->success('成功',url('login/index/index'));
    }


    


}
